import { Skus } from './sku';
let _Skus = Skus;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import SkuMock from './mk/mng/card/skuMock';
 * SkuMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mk/mng/card/sku/listKey').reply(config => {
      let params = config.params;
      let mockSkus = _Skus.filter(sku => {
      	if(params.quantity||params.quantity==''){ if( sku.quantity.indexOf(params.quantity)>=0) return true;}
      	if(params.leastRemindCount||params.leastRemindCount==''){ if( sku.leastRemindCount.indexOf(params.leastRemindCount)>=0) return true;}
      	if(params.cardPkId||params.cardPkId==''){ if( sku.cardPkId.indexOf(params.cardPkId)>=0) return true;}
      	if(params.id||params.id==''){ if( sku.id.indexOf(params.id)>=0) return true;}
      	if(params.totalQuantity||params.totalQuantity==''){ if( sku.totalQuantity.indexOf(params.totalQuantity)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockSkus
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mk/mng/card/sku/list').reply(config => {
      let params = config.params;
      let mockSkus = _Skus.filter(sku => {
      	var isTrue=false;
      	if(params.quantity||params.quantity==''){if(sku.quantity.indexOf(params.quantity)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.leastRemindCount||params.leastRemindCount==''){if(sku.leastRemindCount.indexOf(params.leastRemindCount)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.cardPkId||params.cardPkId==''){if(sku.cardPkId.indexOf(params.cardPkId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.id||params.id==''){if(sku.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.totalQuantity||params.totalQuantity==''){if(sku.totalQuantity.indexOf(params.totalQuantity)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockSkus
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockSkus.length;
      mockSkus = mockSkus.filter((sku, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockSkus,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mk/mng/card/sku/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _Skus = _Skus.filter(sku => sku.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mk/mng/card/sku/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _Skus = _Skus.filter(sku => !params.includes(sku.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mk/mng/card/sku/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { quantity,leastRemindCount,cardPkId,id,totalQuantity } = params;
      _Skus.some(sku => {
        if (sku.id === id) {
			sku.quantity = quantity;
			sku.leastRemindCount = leastRemindCount;
			sku.cardPkId = cardPkId;
			sku.id = id;
			sku.totalQuantity = totalQuantity
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mk/mng/card/sku/add').reply(config => {
      var params = JSON.parse( config.data );
      let { quantity,leastRemindCount,cardPkId,id,totalQuantity } = params;
      _Skus.push({
		quantity:quantity,
		leastRemindCount:leastRemindCount,
		cardPkId:cardPkId,
		id:id,
		totalQuantity:totalQuantity
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};